package com.example.framework;

import android.os.Bundle;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;

import com.example.framework.base.BaseFragment;
import com.example.framework.fragment.CommonFrameFragment;
import com.example.framework.fragment.CustomFragment;
import com.example.framework.fragment.OtherFragment;
import com.example.framework.fragment.ThirdPartyFragment;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends FragmentActivity
{
      
      private RadioGroup mRg_main;
      private List<BaseFragment> mBaseFragment;
      
      //* 选中的Fragment的对应的位置
      private int position;
      
      // * 上次切换的Fragment
      private Fragment mContent;
      
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mRg_main = (RadioGroup) findViewById(R.id.rg_main);
            // mRg_main.check(R.id.rb_common_frame);
            //初始化Fragment
            initFragment();
            //设置RadioGroup的监听
            setListener();
      }
      
      private void setListener()
      {
            mRg_main.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
            //设置默认选中常用框架
            mRg_main.check(R.id.rb_common_frame);
      }
      
      class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener
      {
            
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                  if (checkedId == R.id.rb_common_frame)
                  {
                        position = 0;
                  }
                  else if (checkedId == R.id.rb_thirdparty)
                  {
                        position = 1;
                  }
                  else if (checkedId == R.id.rb_custom)
                  {
                        position = 2;
                  }
                  else if (checkedId == R.id.rb_other)
                  {
                        position = 3;
                  }
                  else
                  {
                        position = 0;
                  }
                  
                  
                  //根据位置得到对应的Fragment
                  BaseFragment to = getFragment();
                  //替换
                  switchFrament(mContent, to);
                  
            }
      }
      
      
      /**
       * @param from 刚显示的Fragment,马上就要被隐藏了
       * @param to   马上要切换到的Fragment，一会要显示
       */
      private void switchFrament(Fragment from, Fragment to)
      {
            if (from != to)
            {
                  mContent = to;
                  FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                  //才切换
                  //判断有没有被添加
                  if (!to.isAdded())
                  {
                        //to没有被添加
                        //from隐藏
                        if (from != null)
                        {
                              ft.hide(from);
                        }
                        //添加to
                        if (to != null)
                        {
                              ft.add(R.id.fl_content, to).commit();
                        }
                  }
                  else
                  {
                        //to已经被添加  from隐藏
                        if (from != null)
                        {
                              ft.hide(from);
                        }
                        //显示to
                        if (to != null)
                        {
                              ft.show(to).commit();
                        }
                  }
            }
            
      }
      
      //    private void switchFrament(BaseFragment fragment) {
      //        //1.得到FragmentManger
      //        FragmentManager fm = getSupportFragmentManager();
      //        //2.开启事务
      //        FragmentTransaction transaction = fm.beginTransaction();
      //        //3.替换
      //        transaction.replace(R.id.fl_content, fragment);
      //        //4.提交事务
      //        transaction.commit();
      //    }
      
      /**
       * 根据位置得到对应的Fragment
       *
       * @return
       */
      private BaseFragment getFragment()
      {
            
            BaseFragment fragment = mBaseFragment.get(position);
            return fragment;
      }
      
      private void initFragment()
      {
            mBaseFragment = new ArrayList<>();
            mBaseFragment.add(new CommonFrameFragment());//常用框架Fragment
            mBaseFragment.add(new ThirdPartyFragment());//第三方Fragment
            mBaseFragment.add(new CustomFragment());//自定义控件Fragment
            mBaseFragment.add(new OtherFragment());//其他Fragment
      }
      
      
}